<?php

class Model_Feed extends Joob_Db_Table
{

	public $id;
	public $from_id, $to_id, $is_story, $story_id, $since, $content, $type, $public_id;

	protected $key = "id";
	
	protected $db = "feeds";
	
	public function deleteFeedForUser($userID)	{
		return $this->delete("id = ".$this->id." AND (from_id=$userID OR to_id=$userID)");
	}
	
	public function add(){            
		$this->since=time();
		$success=$this->insert('from_id, to_id, content, is_story, story_id , public_id , since, type');
		if ($success<=0){
			throw new Exception('Connection error');
		} 
		else return true;
	}
	
	public function checkinFeed(){
		$this->since = time();
		$this->public_id = 2;
		$this->is_story = 1;
		$this->type = 'place';
		$this->add();
	}

	public static function getFriendFeeds($viewer_id,$page,$page_size){
		$update = new Model_Feed();
		$user = new Model_User();
		$user->setOption("id", $viewer_id);
		$friends = $user->getFriendIds();
		$in = $viewer_id;
		if(sizeof($friends)>0){
			for($i=0;$i<sizeof($friends);$i++){
				$in.=','.$friends[$i];
			}
		}
		$result = $update->search('is_story = 1 and ((from_id in ('.$in.') and public_id >0) or from_id='.$viewer_id.' or to_id='.$viewer_id.')', '*', ($page*$page_size).','.$page_size,'id DESC');
		//FIXME: Use join

		$feeds = array();
		for($i=0;$i<count($result);$i++)	{
			$feed = $result[$i];
			$user = new Model_User();
			$user->id = $feed->from_id;
			$result1 = $user->connect();
			if ($result1 == false)	{
				continue;
			}
			$feed->fromUser = $user->getInfo();
			
			$user = new Model_User();
			$user->id = $feed->to_id;
			$result1 = $user->connect();
			if ($result1 != false)	{
				$feed->toUser = $user->getInfo();
			}
			$feeds[] = $feed;
		}
		return $feeds;
	}
	
	public static function getUserFeeds($user_id,$viewer_id,$page,$page_size){
		$update = new Model_Feed();
		$friend = new Model_Friend();
		$friend->setOption('user_id', $viewer_id);
		$friend->setOption('friend_id', $user_id);
		if($friend->isFriend()){
			$result = $update->search('is_story = 1 and (((from_id='.$user_id.' or to_id='.$user_id.') and public_id>0) or (from_id='.$viewer_id.' and to_id='.$user_id.') or (from_id='.$user_id.' and to_id='.$viewer_id.'))', '*', $page*$page_size.','.$page_size,'id DESC');
		}
		else{
			$result = $update->search('is_story = 1 and (((from_id='.$user_id.' or to_id='.$user_id.') and public_id>1) or (from_id='.$viewer_id.' and to_id='.$user_id.') or (from_id='.$user_id.' and to_id='.$viewer_id.'))', '*', $page*$page_size.','.$page_size,'id DESC');
		}
		
		$feeds = array();
		for($i=0;$i<count($result);$i++)	{
			$feed = $result[$i];
			$user = new Model_User();
			$user->id = $feed->from_id;
			$result1 = $user->connect();
			if ($result1 == false)	{
				continue;
			}
			$feed->fromUser = $user->getInfo();
			
			$user = new Model_User();
			$user->id = $feed->to_id;
			$result1 = $user->connect();
			if ($result1 != false)	{
				$feed->toUser = $user->getInfo();
			}
			$feeds[] = $feed;
		}
		return $feeds;
	}
	
	public static function getUserToUserFeeds($user_id,$viewer_id,$page,$page_size){
		$update = new Model_Feed();
		return $update->search('is_story = 1 and ((from_id='.$viewer_id.' and to_id='.$user_id.') or (from_id='.$user_id.' and to_id='.$viewer_id.'))', '*', $page*$page_size.','.$page_size,'id DESC');
	}

	public static function getComments($story_id,$page,$page_size){
		$update = new Model_Feed();
		return $update->search('story_id='.$story_id, '*', $page*$page_size.','.$page_size,'id DESC');
	}

	public function deleteComment() {
		$sql = "DELETE FROM feeds WHERE story_id = ".$this->id;
		return SQL::update($sql);
	}

	public function deleteVote(){
		$sql = "DELETE FROM votes WHERE type='feed' AND target_id = ".$this->id;
		return SQL::update($sql);
	}
}
?>